package cn.qdhlm.testrocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.nio.charset.StandardCharsets;

/**
 * @Name:SyncProducer
 * @description: 同步发送
 * @author: xinY
 **/
public class SyncProducer {
    public static void main(String[] args) throws Exception {

        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("syncProducer");
        // Specify name server addresses.
        producer.setVipChannelEnabled(false);
//        producer.setNamesrvAddr("localhost:9876");
        producer.setNamesrvAddr("192.168.0.159:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 100; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("TopicTest", "TagA", ("Hello RocketMQ " + i).getBytes("UTF-8"));
            //Call send message to deliver message to one of brokers.
            Message msg1 = new Message("server1", "Tagfffck", ("sync" + i).getBytes("UTF-8"));
            SendResult sendResult = producer.send(msg1);
            System.out.println("topic:" + msg1.getTopic() + ", tags:" + msg1.getTags() + ", body:" + new String(msg1.getBody()));
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
        System.out.println(6 | 3);
    }

}
